package com.example.yan_music.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.yan_music.R;
import com.example.yan_music.adapter.BannerImageAdapter;
import com.example.yan_music.adapter.MainViewPagerAdapter;
import com.example.yan_music.base.fragment.BaseFragment;
import com.example.yan_music.bean.wangyi.SongListDetail;
import com.example.yan_music.contract.IListDetailContract;
import com.example.yan_music.event.PlayListInfoEvent;
import com.example.yan_music.presenter.IListDetailPresenterImp;
import com.example.yan_music.utils.XLog;
import com.example.yan_music.utils.statusbar.StatusBarUtil;
import com.example.yan_music.view.MainActivity;
import com.example.yan_music.view.MainFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * <p>
 * 歌单详情Fragment
 * </p>
 */
public class ListDetailFragment extends BaseFragment implements MainViewPagerAdapter.OnReplaceable, IListDetailContract.View {

    @BindView(R.id.img_toolbar_back)
    ImageView back;     // 返回
    @BindView(R.id.img_toolbar_more)
    ImageView mMoreOp;  // 更多操作选项
    @BindView(R.id.img_toolbar_search)
    ImageView mSearch;  // 搜索本页面内容


    @BindView(R.id.nsv_list_detail)
    NestedScrollView mNsvScroll;
    @BindView(R.id.tv_toolbar_title)
    TextView mToolbarTitle;     // 顶部标题、歌名切换
    @BindView(R.id.ll_bar)
    RelativeLayout mLlToolbar;


    private static final String TAG = "ListDetailFragment";
    @BindView(R.id.rl_bac)
    RelativeLayout mRlCover;       // 封面布局
    @BindView(R.id.tv_love_counts)
    TextView mTvSubscribedCounts;     // 收藏数
    @BindView(R.id.tv_talk_counts)
    TextView mTvTalkCounts;     // 评论数
    @BindView(R.id.tv_share_counts)
    TextView mTvShareCounts;    // 分享数
    @BindView(R.id.ll_function)
    LinearLayout mLlNumber;     // 中间收藏评论分享布局
    @BindView(R.id.tv_counts)
    TextView mTvSongCounts;     // 歌单总共含有的歌曲数目
    @BindView(R.id.rl_play_bar)
    RelativeLayout mRlPlayBar;  // “播放全部”所在的布局
    @BindView(R.id.rcv_list)
    RecyclerView mRcvList;      // 歌曲列表
    @BindView(R.id.img_list_cover)
    ImageView mImgListCover;
    @BindView(R.id.tv_decription)
    TextView mTvListName;
    @BindView(R.id.img_list_play)
    ImageView imgListPlay;
    @BindView(R.id.tv_play_all)
    TextView tvPlayAll;
    @BindView(R.id.rl_list_toolbar)
    RelativeLayout rlListToolbar;
    @BindView(R.id.tv_d)
    TextView mTvDescription;
    private Unbinder unbinder;
    private MainActivity mMainActivity;

    /**
     * 歌单id
     */
    private long mListId;

    /**
     * 歌单详情中的trackIds集合
     */
    private List<SongListDetail.PlaylistBean.TrackIdsBean> mTrackIdsList;

    private IListDetailPresenterImp iListDetailPresenterImp;

    public ListDetailFragment(long listId) {
        this.mListId = listId;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_detial, container, false);
        initSettings(view);
        XLog.d("listId=" + mListId);
        iListDetailPresenterImp.getPlayListDetail(mListId);
        return view;
    }

    /**
     * 初始设置
     *
     * @param view
     */
    private void initSettings(View view) {
        unbinder = ButterKnife.bind(this, view);
        mMainActivity = (MainActivity) getActivity();
        StatusBarUtil.setTranslucentStatus(mMainActivity);
        iListDetailPresenterImp = new IListDetailPresenterImp();
        iListDetailPresenterImp.attachView(this);
    }


    @OnClick({R.id.img_toolbar_back, R.id.img_toolbar_more, R.id.img_toolbar_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_toolbar_back:
                back();
                break;
            case R.id.img_toolbar_more:
                break;
            case R.id.img_toolbar_search:
                break;
            default:
                break;
        }
    }

    /**
     * 返回上一个页面
     */
    private void back() {
        back(new MainFragment());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 解绑黄油刀
        unbinder.unbind();
        iListDetailPresenterImp.detachView();
    }

    /**
     * 监听整体的滑动，做出对应的ui变化
     * 1.背景渐变
     * 2.标题变为可滚动的歌单描述
     * 3.toolbar布局背景渐变
     * 4.功能区（收藏，评论，分享）上移，可见度变为0
     */
    private void uiByNsvScroll() {
        mNsvScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int dY = scrollY - oldScrollY;
                if (dY > 0) {
                    // 上滑
                } else {
                    // 下滑
                }
            }
        });
    }

    @Override
    public void setTrackIds(SongListDetail.PlaylistBean playlistBean) {
        List<SongListDetail.PlaylistBean.TrackIdsBean> trackIdsBeanList = playlistBean.getTrackIds();
        StringBuffer ids = new StringBuffer();
        // todo 拿全部的ids一次性请求所有歌曲的详情song/detail
        for (int i = 0; i < trackIdsBeanList.size(); i++) {
            String id = trackIdsBeanList.get(i).getId() + ",";
            ids.append(id);
        }
        Log.d(TAG, "setTrackIds: " + ids);
        //iListDetailPresenterImp.getSongDetail(ids.toString());

        int playCounts = playlistBean.getPlayCount();   // 播放量
        int shareCounts = playlistBean.getShareCount(); // 分享量
        int commentCount = playlistBean.getCommentCount(); // 评论量
        int subscribedCounts = playlistBean.getSubscribedCount();   // 订阅量
        String listName = playlistBean.getName();   // 歌单名字
        String description = playlistBean.getDescription(); // 歌单描述
        String coverImgUrl = playlistBean.getCoverImgUrl(); // 歌单封面地址

        Log.d(TAG, "setTrackIds: " +
                "\n" + listName +
                "\n" + description+
                "\n" + coverImgUrl);

        mTvShareCounts.setText(shareCounts + "");
        mTvTalkCounts.setText(commentCount + "");
        mTvSubscribedCounts.setText(subscribedCounts + "");
        mTvDescription.setText(description);
        mTvListName.setText(listName);
        Glide.with(mMainActivity)
                .load(coverImgUrl)
                .placeholder(R.drawable.pre_list_cover)
                .transform(new BannerImageAdapter.GlideRoundTransform(mMainActivity))
                .into(mImgListCover);

    }

}